package com.fit.pages.payment;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.payment.PaymentDAO;
import com.fit.dao.registration.LicencePlateDAO;
import com.fit.dao.registration.RegistrationDAO;
import com.fit.entities.Payment;
import com.fit.entities.LicencePlate;
import com.fit.entities.Registration;
import com.fit.model.glov.GenericSelectModel;
import com.fit.pages.registration.ManageRegistrations;

@UserAccess
@AdminAccess
public class AddPayment {

	@Property
	@SessionState(create = false)
	private Payment payment;

	@Inject
	private PaymentDAO paymentDAO;

	@Inject
	private PropertyAccess access;

	@Property
	@Persist
	private Registration registration;

	@Inject
	private RegistrationDAO registrationDAO;
	
	@Property
	@Persist
	private LicencePlate licencePlate;

	@Inject
	private LicencePlateDAO licencePlateDAO;

	private GenericSelectModel<Registration> registrations;

	@InjectPage
	private ManageRegistrations manage;

	@Persist
	@Property
	private String error;

	@Component
	private Form createForm;

	@Property
	@Persist
	private double paid;

	@Persist
	private String lp;

	@Property
	@Persist
	private boolean allPaid;

	@Property
	@Persist
	private List<Payment> payments;
	
	@Property
	private Payment paymentRow;
	

	Object onActivate(String licencePlateNo) {
		allPaid = false;
		payment = new Payment();
		LicencePlate l = licencePlateDAO.findByLicencePlateNo(licencePlateNo);
		registration = registrationDAO.findLast(l);
		lp = l.getLicencePlateNo();
		payments = (ArrayList<Payment>) paymentDAO.findByRegistration(registration);
		paid = 0;
		for (int i = 0; i < payments.size(); i++) {
			paid = paid + payments.get(i).getAmount();
		}
		if (paid == registration.getPrice()) {
			allPaid = true;
		}
		return null;
	}

	Object onSuccess() {
		Calendar c = Calendar.getInstance();
		LicencePlate l = licencePlateDAO.findByLicencePlateNo(lp);
		Registration r = registrationDAO.findLast(l);
		if (payment.getAmount() > r.getPrice() - paid) {
			error = "Amount is greater than debt";
			registration = r;
			return null;
		}
		payment.setDate(new Date());
		payment.setRegistration(r);
		if (paymentDAO.save(payment)) {
			manage.setConfirmation("Payment successfully inserted!");
			return manage;
		}
		manage.setError("Payment is null!");
		return manage;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		error = "";
		return null;
	}

	public boolean isErrorNotNull() {
		if (error == null || error == "") {
			return false;
		}
		return true;
	}

}
